// mass: g/cm, young: kgf/mm2
var STRINGS = {"スチール": {"mass": "7.8", "young": "19600"},
			   "ナイロン": {"mass": "1.14", "young": "180"},
			   "ザイロン": {"mass": "1.54"},
			   "フロロ":   {"mass": "1.78", "young": "192"}};
var TONES =	{"E2":   "82.407", 
			 "A2":  "110.0",
			 "D3":  "146.832", 
			 "G3":  "195.998", 
			 "B3":  "246.942", 
			 "E4":  "329.63",
			 "C4":  "261.626",
			 "C#4": "277.183",
			 "D4":  "293.665",
			 "D#4": "311.127",
			 "E4":  "329.628",
			 "F4":  "349.228",
			 "F#4": "369.994",
			 "G4":  "391.995",
			 "G#4": "415.305",
			 "A4":  "440",
			 "A#4": "466.164",
			 "B4":  "493.883",
			 "B4":  "523.251"}; 

function Tension(material, gauge, length, tone) {
	this.material = material;
	this.gauge = gauge;
	this.length = length;
	this.tone = tone;
	this.calc();
	return this;
}
Tension.prototype.calc = function() {
	if (!this.isNumerical())
		return Number.NaN;
	var s = (2.54 * this.gauge / 2);
	this.lengthMass = 3.14159 * STRINGS[this.material]['mass'] * s * s;
	var tmp = 2 * this.length * TONES[this.tone];
	var tension = this.lengthMass * tmp * tmp / 980;
	return this.tension = Math.round(tension) / 1000;
};
Tension.prototype.isNumerical = function() {
	if (STRINGS[this.material] == null
		|| isNaN(parseFloat(STRINGS[this.material]['mass']))
		|| isNaN(parseFloat(this.gauge))
		|| isNaN(parseFloat(this.length))
		|| isNaN(parseFloat(TONES[this.tone])))
		return false;
	return true;
};

function Wound(lengthMass, length, tone) {
	this.lengthMass = lengthMass;
	this.length = length;
	this.tone = tone;
	this.calc();
	return this;
}
Wound.prototype.calc = function() {
	if (!this.isNumerical())
		return Number.NaN;
	var tmp = 2 * this.length * TONES[this.tone];
	var tension = this.lengthMass * tmp * tmp / 980;
	return this.tension = Math.round(tension) / 1000;
};
Wound.prototype.isNumerical = function() {
	if (isNaN(parseFloat(this.lengthMass))
		|| isNaN(parseFloat(this.length))
		|| isNaN(parseFloat(TONES[this.tone])))
		return false;
	return true;
};

displays = [];
function Display(id, tension) {
	this.id = id;
	this.tension = tension;
	this.tag = '<table>\
  <tr><td>素材</td>\
    <td><select id="' + id + ':material" onChange="displays[\'' + id + '\'].changeMaterial();"></select></td></tr>\
  <tr><td>密度</td>\
    <td><input size="5" id="' + id + ':mass" /> g/cm</td></tr>\
  <tr><td>直径</td>\
    <td><input id="' + id + ':gauge" /> inch</td></tr>\
  <tr><td>弦長</td>\
    <td><input size="5" id="' + id + ':length" value="35" /> cm</td></tr>\
  <tr><td>音名</td>\
    <td><select id="' + id + ':tone" onChange="displays[\'' + id + '\'].changeTone()"></select> tone</td></tr>\
  <tr><td><input type="button" onClick="displays[\'' + id + '\'].calc();" value="計算"></td>\
    <td><input size="5" id="' + id + ':result" /> kg</td></tr>\
</table>';
	displays[id] = this;
	$(id).innerHTML = this.tag;

	this.material = $(this.id + ':material');
	this.mass = $(this.id + ':mass');
	this.gauge = $(this.id + ':gauge'); 
	this.length = $(this.id + ':length');
	this.tone = $(this.id + ':tone');
	this.result = $(this.id + ':result');

	this.listMaterial();
	if (!isNaN(parseFloat(this.tension.gauge)))
		this.gauge.value = this.tension.gauge;
	if (!isNaN(parseFloat(this.tension.length)))
		this.length.value = this.tension.length;
	this.listTone();
	this.calc();

	return this;
}
Display.prototype.listMaterial = function() {
	for (var m in STRINGS) {
		var option = new Option(m);
		if (m == this.tension.material)
			option.selected = true;
		this.material.appendChild(option);
	}
	this.mass.value = STRINGS[this.material.value]['mass'];
};
Display.prototype.listTone = function() {
	while(this.tone.hasChildNodes())
		this.tone.removeChild(this.tone.firstChild);
	for (var t in TONES) {
		var option = new Option(t);
		if (t == this.tension.tone)
			option.selected = true;
		this.tone.appendChild(option);
	}
};
Display.prototype.changeMaterial = function() {
	this.mass.value = STRINGS[this.material.value]['mass'];
	this.calc();
};
Display.prototype.changeTone = function() {
	this.calc();
};
Display.prototype.calc = function() {
	this.tension.material = this.material.value;
	this.tension.gauge = this.gauge.value;
	this.tension.length = this.length.value;
	this.tension.tone = this.tone.value;
	//alert('material=' + this.tension.material);
	//alert('gauge=' + this.tension.gauge);
	//alert('length=' + this.tension.length);
	//alert('tone=' + this.tension.tone);
	//alert('freq=' + TONES[this.tension.tone]);
	this.result.value = this.tension.calc();
};
